/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.esgi.dns.client;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

import org.esgi.dns.Header;
import org.esgi.dns.Question;

/**
 *
 * @author amine
 */
public class Client {
        public static byte [] _prepareRequest(){
            Header header = new Header();
            header.setAa(false);
            header.setAncount((short)-1);
            header.setId((short)128);
            header.setNscount((short)2);
            header.setOpcode((short)14);
            header.setQdcount((short)-127);
            header.setRa(true);
            header.setQr(true);
            header.setRcode((short)12);
            header.setRd(true);
            header.setTc(true);
            header.setZ((short)3);
            ByteBuffer encoded = header.encode();
            Question question = new Question();
            question.setqName("www.google.com");
            question.setqType((short)12);
            question.setqClass((short)12);
            ByteBuffer qEncoded = null;
            try {
				 qEncoded = question.encode(encoded);
			} catch (Exception e) {
				e.printStackTrace();
			}
           return qEncoded.array();
        }
        public static void main(String [] args) throws UnknownHostException, IOException{
            System.out.println("send request...");
            byte[] header =_prepareRequest();
            Socket socket = new Socket("localhost", 6789);
            socket.getOutputStream().write(header);
            InputStream is = socket.getInputStream();
            System.out.println("get response ...");
            int c=-1;
            while(-1 != (c=is.read())){
                System.out.print((char)c);
            }
           // socket.close();
        }
}
